package cn.hd.service.impl;


import cn.hd.dao.BillDao;
import cn.hd.dao.ProductdetailDao;
import cn.hd.entity.Bill;
import cn.hd.entity.Productdetail;
import cn.hd.service.BillService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class BillServiceImpl implements BillService {
    @Autowired
    private BillDao billDao;
    @Autowired
    private ProductdetailDao productdetailDao;
    @Override
    public PageInfo<Bill> selectAllBill(int pagenum, int pagesize,Map map) {
        PageHelper.startPage(pagenum,pagesize);
        List<Bill> bills = billDao.selectAllBill(map);
        Date data = new Date();
        for (Bill b:bills){
            if (b.getEnddate().before(data)){
                b.setJiaoyistatu("交易完成");
            }else {
                b.setJiaoyistatu("交易ing");
            }
            int i = billDao.selectZCStatus(b.getUuid());
            if(i == 0){
                b.setZcstatename("众筹中");
            }else if(i == 1){
                b.setZcstatename("众筹成功");
            }else if(i == 2){
                b.setZcstatename("众筹失败");
            }
        }
        PageInfo<Bill> page = new PageInfo<>(bills);
        return page;
    }

    @Override
    public int addBill(Bill bill) {
        String s = UUID.randomUUID().toString().replaceAll("-", "");
        bill.setUuid(s);
        Productdetail productdetail = productdetailDao.selectAllByXiamgMuname(bill.getProductname());
        bill.setBegindate(productdetail.getOverdate());

        Calendar calendar =  Calendar.getInstance();
        calendar.setTime(bill.getBegindate());
        calendar.add(Calendar.DATE,15);
        bill.setEnddate(calendar.getTime());

        return billDao.addBill(bill);
    }

    @Override
    public int deleteBill(int billid) {
        return billDao.deleteBill(billid);
    }

    @Override
    public Bill selectBillByBid(int billid) {
        return billDao.selectBillByBid(billid);
    }

    @Override
    public int addBillAddress(long bid, long aid) {
        return billDao.addBillAddress(bid,aid);
    }

    @Override
    public int addBillUser(long bid, int uid) {
        return billDao.addBillUser(bid,uid);
    }

    @Override
    public int wsbill(String billname, int bid) {
        return billDao.wsbill(billname,bid);
    }

    @Override
    public List<Bill> selectAllDD() {
        return billDao.selectAllDD();
    }

    @Override
    public int updateDDZT(String uuid) {
        return billDao.updateDDZT(uuid);
    }

    @Override
    public int updateDDZTWZF(String uuid) {
        return billDao.updateDDZTWZF(uuid);
    }

    @Override
    public int selectDDId() {
        return billDao.selectDDId();
    }

    @Override
    public int updateDDZTWZFByid(int bid) {
        return billDao.updateDDZTWZFByid(bid);
    }

    @Override
    public Bill selectDDByuuid(String uuid) {
        return billDao.selectDDByuuid(uuid);
    }

}
